Skip to content

Fix API route middleware path for Milky#653

Merged
linyuchen merged 1 commit into
LLOneBot:mainfrom
lgc2333:patch-1
Dec 12, 2025
Merged

Fix API route middleware path for Milky#653
linyuchen merged 1 commit into
LLOneBot:mainfrom
lgc2333:patch-1

Conversation

@lgc2333
Copy link
Copy Markdown
Contributor

@lgc2333 lgc2333 commented Dec 12, 2025

Summary by Sourcery

Bug Fixes:

  • 修复 API 路由中间件的注册方式,使访问令牌检查能够正确应用于 API 请求,而不再依赖通配符路径段。
Original summary in English

Summary by Sourcery

Bug Fixes:

  • Fix API route middleware registration so access token checks apply correctly to API requests without relying on a wildcard path segment.

@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented Dec 12, 2025

审阅者指南(在小型 PR 上折叠显示)

审阅者指南

调整 Milky HTTP 处理器的访问令牌中间件,使其挂载到基础的 /api 路由,而不是使用通配符路径,从而确保 API 路由能够正确匹配并执行中间件。

API 请求通过更新后的 Milky 访问令牌中间件的顺序图

sequenceDiagram
    actor ApiClient
    participant MilkyHttpHandler
    participant ExpressApp
    participant AccessTokenMiddleware
    participant ApiRouteHandler

    ApiClient->>ExpressApp: HTTP request /prefix/api/resource
    ExpressApp->>MilkyHttpHandler: Initialize with config.prefix and config.accessToken
    MilkyHttpHandler->>ExpressApp: use(config.prefix + /api, AccessTokenMiddleware)

    ApiClient->>ExpressApp: Request /prefix/api/resource
    ExpressApp->>AccessTokenMiddleware: Invoke for /prefix/api

    AccessTokenMiddleware->>AccessTokenMiddleware: Check header content-type === application/json
    alt InvalidContentType
        AccessTokenMiddleware-->>ApiClient: 4xx error response
    else ValidContentType
        AccessTokenMiddleware->>AccessTokenMiddleware: Validate access token using config.accessToken
        alt TokenInvalid
            AccessTokenMiddleware-->>ApiClient: 401 unauthorized
        else TokenValid
            AccessTokenMiddleware->>ExpressApp: next()
            ExpressApp->>ApiRouteHandler: Handle /prefix/api/resource
            ApiRouteHandler-->>ApiClient: 2xx success response
        end
    end
Loading

MilkyHttpHandler HTTP 中间件注册的类图

classDiagram
    class MilkyHttpHandler {
        - any app
        - MilkyConfig config
        + MilkyHttpHandler(app, config)
        + registerAccessTokenMiddleware()
    }

    class MilkyConfig {
        + string prefix
        + string accessToken
    }

    class ExpressApp {
        + use(path, middleware)
    }

    MilkyHttpHandler o-- MilkyConfig : uses
    MilkyHttpHandler o-- ExpressApp : configures

    MilkyHttpHandler : registerAccessTokenMiddleware()

    %% Updated registration behavior
    MilkyHttpHandler ..> ExpressApp : use(config.prefix + /api, middleware)
Loading

文件级变更

变更 详情 文件
更新了 Milky API 路由访问令牌中间件的挂载路径,去除了通配符片段。
  • 将 Express 中间件注册方式从使用 ${config.prefix}/api/* 模式改为 ${config.prefix}/api,使其匹配基础 API 路径而不是通配符路径
  • 保持中间件回调内部现有的访问令牌检查和日志行为不变
src/milky/network/http.ts

提示与命令

与 Sourcery 交互

  • 触发新审阅: 在 pull request 中评论 @sourcery-ai review
  • 继续讨论: 直接回复 Sourcery 的审阅评论。
  • 从审阅评论生成 GitHub Issue: 通过回复某条审阅评论来让 Sourcery 从该评论创建 issue。你也可以回复该评论 @sourcery-ai issue 来从中创建 issue。
  • 生成 pull request 标题: 在 pull request 标题的任意位置写上 @sourcery-ai,即可随时生成标题。你也可以在 pull request 中评论 @sourcery-ai title 来(重新)生成标题。
  • 生成 pull request 摘要: 在 pull request 正文的任意位置写上 @sourcery-ai summary,即可在该位置随时生成 PR 摘要。你也可以在 pull request 中评论 @sourcery-ai summary 来(重新)生成摘要。
  • 生成审阅者指南: 在 pull request 中评论 @sourcery-ai guide,即可在任意时间(重新)生成审阅者指南。
  • 解决所有 Sourcery 评论: 在 pull request 中评论 @sourcery-ai resolve,即可一次性解决所有 Sourcery 评论。当你已经处理完所有评论且不想再看到它们时非常有用。
  • 忽略所有 Sourcery 审阅: 在 pull request 中评论 @sourcery-ai dismiss,即可忽略所有现有的 Sourcery 审阅。特别适用于你想从头开始一次新的审阅——别忘了再评论 @sourcery-ai review 来触发新的审阅!

自定义你的使用体验

访问你的控制面板以:

  • 启用或禁用审阅功能,例如 Sourcery 生成的 pull request 摘要、审阅者指南等。
  • 更改审阅语言。
  • 添加、删除或编辑自定义审阅指令。
  • 调整其他审阅设置。

获取帮助

Original review guide in English
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adjusts the Milky HTTP handler’s access token middleware to attach to the base /api route instead of using a wildcard path, ensuring proper middleware matching and execution for API routes.

Sequence diagram for API request passing through updated Milky access token middleware

sequenceDiagram
    actor ApiClient
    participant MilkyHttpHandler
    participant ExpressApp
    participant AccessTokenMiddleware
    participant ApiRouteHandler

    ApiClient->>ExpressApp: HTTP request /prefix/api/resource
    ExpressApp->>MilkyHttpHandler: Initialize with config.prefix and config.accessToken
    MilkyHttpHandler->>ExpressApp: use(config.prefix + /api, AccessTokenMiddleware)

    ApiClient->>ExpressApp: Request /prefix/api/resource
    ExpressApp->>AccessTokenMiddleware: Invoke for /prefix/api

    AccessTokenMiddleware->>AccessTokenMiddleware: Check header content-type === application/json
    alt InvalidContentType
        AccessTokenMiddleware-->>ApiClient: 4xx error response
    else ValidContentType
        AccessTokenMiddleware->>AccessTokenMiddleware: Validate access token using config.accessToken
        alt TokenInvalid
            AccessTokenMiddleware-->>ApiClient: 401 unauthorized
        else TokenValid
            AccessTokenMiddleware->>ExpressApp: next()
            ExpressApp->>ApiRouteHandler: Handle /prefix/api/resource
            ApiRouteHandler-->>ApiClient: 2xx success response
        end
    end
Loading

Class diagram for MilkyHttpHandler HTTP middleware registration

classDiagram
    class MilkyHttpHandler {
        - any app
        - MilkyConfig config
        + MilkyHttpHandler(app, config)
        + registerAccessTokenMiddleware()
    }

    class MilkyConfig {
        + string prefix
        + string accessToken
    }

    class ExpressApp {
        + use(path, middleware)
    }

    MilkyHttpHandler o-- MilkyConfig : uses
    MilkyHttpHandler o-- ExpressApp : configures

    MilkyHttpHandler : registerAccessTokenMiddleware()

    %% Updated registration behavior
    MilkyHttpHandler ..> ExpressApp : use(config.prefix + /api, middleware)
Loading

File-Level Changes

Change Details Files
Updated the access token middleware mounting path for Milky API routes to remove the wildcard segment.
  • Changed the Express middleware registration from using the ${config.prefix}/api/* pattern to ${config.prefix}/api so it matches the base API route rather than a wildcard path
  • Kept the existing access-token check and logging behavior unchanged within the middleware callback
src/milky/network/http.ts

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你好——我已经审查了你的修改,看起来非常棒!


Sourcery 对开源项目是免费的——如果你喜欢我们的代码审查,请考虑分享给其他人 ✨
帮我变得更有用!请在每条评论上点 👍 或 👎,我会根据你的反馈来改进之后的代码审查。
Original comment in English

Hey there - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@linyuchen linyuchen merged commit ef54a70 into LLOneBot:main Dec 12, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants